<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="25%"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" class="demo-ruleForm">
        <el-form-item label="数据模板" prop="data_id">
          <el-select
            v-model="ruleForm.data_id"
            placeholder="请选择"
            style="width: 100%"
            @change="handleChange"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="参数键值" prop="mqtt_key">
          <el-input v-model="ruleForm.mqtt_key" clearable placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm">取消</el-button>
          <el-button type="primary" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { dataAllList } from '@/api/dataType'
import { detailTemplateData, updateTemplateData, createTemplateData } from '@/api/template'
export default {
  name: 'child',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    },
    tempId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      options: [],
      ruleForm: {
        temp_id: '',
        data_id: '',
        data_name: '',
        column_name: '',
        mqtt_key: ''
      },
      rules: {
        data_id: [
          { required: true, message: '请选择模板参数', trigger: 'blur' }
        ],
        mqtt_key: [
          { required: true, message: '请输入参数键值', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getOptions()
    if (this.title === '新增') {
      this.ruleForm.temp_id = this.tempId
    } else {
      this.getDetailById(this.id)
    }
  },
  methods: {
    // 获取详情
    getDetailById(id) {
      detailTemplateData({ id }).then(res => {
        this.ruleForm.data_name = res.data.temp_name
        this.ruleForm.data_id = res.data.data_id
        this.ruleForm.column_name = res.data.column_name
        this.ruleForm.temp_id = res.data.temp_id
        this.ruleForm.mqtt_key = res.data.mqtt_key
        this.ruleForm.id = res.data.id
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            //  是编辑
            updateTemplateData(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createTemplateData(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    handleChange(e) {
      for (let i = 0; i < this.options.length; i++) {
        if (this.options[i].id === e) {
          this.ruleForm.data_name = this.options[i].label_name
          this.ruleForm.column_name = this.options[i].column_name
          this.ruleForm.data_id = e
          return
        }
      }
    },
    // 获取参数类型数据
    getOptions() {
      dataAllList({
        sort: 'created_at',
        offset: 0,
        limit: 50,
        order: 'ascending',
        type_name: '',
        pid: 0
      }).then(res => {
        this.options = res.data.list
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
